package tree_util;

import com.opensymphony.xwork2.ActionSupport;
import db_tool.DBConnection;
import org.apache.struts2.ServletActionContext;
import org.util.MD5Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 妫�娴嬬敤鎴锋槸鍚﹀瓨鍦�,瀵嗙爜鏄惁姝ｇ‘
 * */
public class Check extends ActionSupport {

	private String username;
	private String password;
	private Account account = null;
	private DBConnection db_connection = null;
	private PrintWriter writer = null;
	
	@Override
	public String execute() {
		// TODO Auto-generated method stub
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		String query_username = "select username from Account";
		account = new Account();
		boolean query_flag = false;
		
		String check_active = "select active from Account where username='" + username
				+ "'";
		String acti=null;
		
		String check_password = "select * from Account where username='" + username
				+ "'";
		boolean check_flag = false;
		
		db_connection = DBConnection.getInstance();
		try {
			writer = ServletActionContext.getResponse().getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Statement statement = null;
		ResultSet resultSet = null;
		
		try {
			statement = db_connection.getStatement();
			resultSet = statement.executeQuery(check_password);
			while (resultSet.next()){
				String checkPasswold =  resultSet.getString("passwold");
				String checkActive = resultSet.getString("active");
				String id = resultSet.getString("id");
				String telephone = resultSet.getString("Telephone");
				String email = resultSet.getString("Email");
				String description = resultSet.getString("Description");
				String active = resultSet.getString("active");
				String superiorName = resultSet.getString("superiorName");
				String roleName = resultSet.getString("roleName");
				if (checkPasswold.length() != 32) {
					checkPasswold = MD5Util.getMD5(checkPasswold);
					String updateSql = "update Account set passwold='"+checkPasswold+"' where username='"+username+"'";
					statement.execute(updateSql);
				}
				password = MD5Util.getMD5(password);
				System.out.println(password);
				System.out.println(checkPasswold);
				if(!checkPasswold.equals(password)){
					writer.write("passwdErr");  
					return null;
				}
				if(checkActive.equals("no")){
					writer.write("noactive");
					return null;
				}
				if(checkActive.equals("out")){
					writer.write("outofdate");
					return null;
				}
				account.setID(id);
				account.setUsername(username);
				account.setTelephone(telephone);
				account.setEmail(email);
				account.setDescription(description);
				account.setActive(active);
				account.setSuperiorName(superiorName);
				account.setRoleName(roleName);
				ServletActionContext.getRequest().getSession().setAttribute("account",account);
                return null;
				
			}
			writer.write("nouserErr");
			/*
			while (resultSet.next()) {
				query_flag = true;//鏈夌敤鎴�
			}
			
			if(!query_flag){			//娌℃湁鐢ㄦ埛,鐩存帴杩斿洖
				writer.write("nouserErr");
				return null;
			}
			
			resultSet = statement.executeQuery(check_active);
			System.out.println("eeeeeeeeeeeeeeeeeeeee");
			while(resultSet.next()){
				acti=resultSet.getString("active");
				System.out.println(acti);
				System.out.println("wwwwwwwwwwwwwwwwwwwww");
			}
			if(acti.equals("no")){         //娌℃湁琚縺娲�
				writer.write("noactive");
			}
			
			
			resultSet = statement.executeQuery(check_password);
			while (resultSet.next()) {
				check_flag = true;				//璐︽埛,瀵嗙爜姝ｇ‘
			}
			
			if(!check_flag){
				writer.write("passwdErr");
			}
			
			*/
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			writer.write("error");
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			writer.write("error");
			e.printStackTrace();
		} finally{
			try {
				db_connection.close(resultSet,statement);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
